﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WodLib
{

    /// <summary>
    /// Represents WoD animal
    /// </summary>
    [Serializable()]
    public class Animal : ConciousBeing
    {
        #region HumanBasicTraits

        [System.ComponentModel.Category(Config.DescriptionConsts.Traits)]
        public HumanAttributes Attributes { get; set; }

        [System.ComponentModel.Category(Config.DescriptionConsts.Traits)]
        public HumanSkills Skills { get; set; }

        public HumanInventory Inventory
        {
            get;
            set;
        }
        #endregion

        #region Advantages

        [System.ComponentModel.Category(Config.DescriptionConsts.OtherTraits)]
        public override int Speed
        {
            get
            {
                return Attributes.Strength.CurrentValue + Attributes.Dexterity.CurrentValue + SpeedSpieciesFactor;
            }
            set
            {

            }
        }

        public int SpeedSpieciesFactor { get; set; }

        public override int InitiativeModifier
        {
            get
            {
                return Attributes.Dexterity.CurrentValue + Attributes.Composure.CurrentValue;
            }
            set
            {

            }
        }

        public override int Defense
        {
            get
            {
                return Math.Max(Attributes.Dexterity.CurrentValue, Attributes.Witts.CurrentValue);
            }

            set
            {

            }
        }

        public override int WillpowerDots
        {
            get
            {
                return Attributes.Resolve.CurrentValue + Attributes.Composure.CurrentValue;
            }
        }

        #endregion

        #region AnimalAdvancedTraits

        [System.ComponentModel.Category(Config.DescriptionConsts.OtherTraits)]
        public int Armor { get; set; }
        #endregion

        #region WodObjectProperties
        private int size = 0;

        [System.ComponentModel.Category(Config.DescriptionConsts.OtherTraits)]
        public override int Size
        {
            get
            {
                return this.size;
            }
            set
            {
                this.size = value;
                this.SetBaseHealth();
            }
        }
        #endregion

        /// <summary>
        /// Creates deafault Animal
        /// </summary>
        public Animal():base()
        {
            Attributes = new HumanAttributes();
            Skills = new HumanSkills();

            Inventory = new HumanInventory();
            Health.SetBaseMaxHealth(Size + Attributes.Stamina.CurrentValue);
            Attributes.AnyHasChanged += new EventHandler(Attributes_AnyHasChanged);
        }


        #region Privates

        void Attributes_AnyHasChanged(object sender, EventArgs e)
        {
            WodLib.Attribute attr = (sender as WodLib.Attribute);
            if (attr != null)
            {
                if (attr.Cathegory == AttributeCathegory.Resistance && attr.TypeOfTrait == TraitType.Physical)
                {
                    this.SetBaseHealth();
                }
            }
        }

        private void SetBaseHealth()
        {
            Health.SetBaseMaxHealth(Attributes.Stamina.CurrentValue + this.Size);
        } 
        #endregion
        
        public override object Clone()
        {
            Animal tmp = (Animal) base.Clone();
            tmp.Inventory = (HumanInventory)this.Inventory.Clone();
            tmp.Attributes = (HumanAttributes)this.Attributes.Clone();
            tmp.Skills = (HumanSkills)this.Skills.Clone();
            return tmp;
        }


        #region ITraitable Members

        public override Dictionary<string, int> GetIntegerTraits()
        {
            var dict = base.GetIntegerTraits();
            //add attributes
            var attribDict = WodLib.Tools.TraitDictionaryLister<WodLib.Attribute>.FindAllTraits(this.Attributes);
            foreach (var item in attribDict)
            {
                dict.Add(item.Key, item.Value.CurrentValue);
            }
            //add skills
            var skillDict = WodLib.Tools.TraitDictionaryLister<WodLib.Skill>.FindAllTraits(this.Skills);
            foreach (var item in skillDict)
            {
                dict.Add(item.Key, item.Value.Value);
            }
            return dict;
        }

        #endregion

    }
}
